/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* MpmProjectBidScoreFeedBackService
* 评标细则反馈表
*
* 修改纪录
* 2022-05-24 版本：1.0 hudeyong 创建。
* @version 版本：1.0
* @author 作者：hudeyong
* 创建日期：2022-05-24
*/
package com.topscomm.mpm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.pub.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectBidScoreFeedBackService;
import com.topscomm.mpm.service.impl.auto.MpmProjectBidScoreFeedBackServiceAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.pojo.MpmProjectBidScoreEntity;
import com.topscomm.mpm.pojo.MpmProjectBidScoreFeedBackEntity;
import com.topscomm.mpm.pojo.MpmProjectBidScoreHisEntity;

@Service(value = "mpmProjectBidScoreFeedBackService")
@Scope(value = "singleton")
public class MpmProjectBidScoreFeedBackService extends MpmProjectBidScoreFeedBackServiceAuto implements IMpmProjectBidScoreFeedBackService {
	@Autowired
	MpmProjectBidScoreService mpmProjectBidScoreService;
	@Autowired
	MpmProjectVisitExpertService mpmProjectVisitExpertService;
	@Autowired
	private MpmProjectProductInfoService mpmProjectProductInfoService;
	/**
	 * @author: hudeyong
	 * @date: 2022-05-24
	 * @param entity
	 * @throws ServiceException
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(MpmProjectBidScoreFeedBackEntity.FieldDocno, docNo);
		}
	}

	/**
	 * @author: hdy
	 * @date: 2020年05月26日
	 * @param entity
	 * @throws ServiceException
	 * @return_type: void
	 * @description:新增后处理
	 * @modify:
	 */
	protected void afterInsert(BasicEntity entity) throws ServiceException {
		MpmProjectBidScoreFeedBackEntity bidScoreFeedBackEntity = (MpmProjectBidScoreFeedBackEntity) entity;
		String usercode = bidScoreFeedBackEntity.getUsercode();
		String sourceid = ConvertUtil.convertToString(bidScoreFeedBackEntity.getSourceid());
		MpmProjectBidScoreEntity mpmProjectBidScoreEntity = mpmProjectBidScoreService.queryById(sourceid);
		if(mpmProjectBidScoreEntity.getUsercode().equals(usercode)) {
			mpmProjectBidScoreEntity.setCompletion(bidScoreFeedBackEntity.getCompletion());
			mpmProjectBidScoreEntity.setCompletionscore(bidScoreFeedBackEntity.getCompletionscore());
			mpmProjectBidScoreEntity.setImprovescore(bidScoreFeedBackEntity.getImprovescore());
			mpmProjectBidScoreService.updateEntity(mpmProjectBidScoreEntity);
		}
	}

	/**
	 *
	 * @description: 审批通过后的处理
	 * @param billEntity
	 * @param nodeIns
	 * @param node
	 * @param sessionUserBean
	 * @author: hdy
	 * @date: 2022年4月26日
	 * @modify:
	 */
	@Override
	protected void afterApprove(TapEntity billEntity, CboApprovalNodeInsEntity nodeIns, CboApprovalNodeEntity node,
			SessionUserBean sessionUserBean) {
		String id = ConvertUtil.convertToString(nodeIns.getSourceid());
		MpmProjectBidScoreFeedBackEntity bidScoreFeedBackEntity = this.queryById(id);
		if(bidScoreFeedBackEntity == null) {
			return;
		}
		String state = ConvertUtil.convertToString(bidScoreFeedBackEntity.getState());
		String sourceid = ConvertUtil.convertToString(bidScoreFeedBackEntity.getSourceid());
		String completion = bidScoreFeedBackEntity.getCompletion();
		String completionscore = bidScoreFeedBackEntity.getCompletionscore();
		String improvescore = bidScoreFeedBackEntity.getImprovescore();
		// 如果审批通过，更新评标主表
		if( "9".equals(state)) {
			MpmProjectBidScoreEntity mpmProjectBidScoreEntity = mpmProjectBidScoreService.queryById(sourceid);
			mpmProjectBidScoreEntity.setCompletion(completion);
			mpmProjectBidScoreEntity.setCompletionscore(completionscore);
			mpmProjectBidScoreEntity.setImprovescore(improvescore);
			mpmProjectBidScoreService.updateEntity(mpmProjectBidScoreEntity);
		}
		super.afterApprove(billEntity, nodeIns, node, sessionUserBean);
	}
	/**
	 * @description:投标审批人查询
	 * @author: houyuhang
	 * @date: 2022年11月28日
	 * @modify:
	 */
	public List<String> queryBidUserForBidScore(MpmProjectEntity mpmProjectEntity) {
		List<String> userList = new ArrayList<>();
		String projectlevel = mpmProjectEntity.getProjectlevel();
		// A级项目金毅
		if (MpmSystemConst.ProjectLevel.A.equals(projectlevel)) {
			userList.add("0335");
		}
		// B级项目，根据产品线进行对应人员赋值
		if (MpmSystemConst.ProjectLevel.B.equals(projectlevel)) {
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" projectid = '" + mpmProjectEntity.getId() + "'");
			List<Map<String, Object>> list = mpmProjectProductInfoService.queryMapByWhere(sql.toString());
			if (!CollectionUtil.isEmpty(list)) {
				String productLineCode = list.stream().filter(item -> !ObjectUtil.isEmpty(item.get("productlinecode"))).map(item -> ConvertUtil.convertToString(item.get("productlinecode"))).collect(Collectors.joining(","));
				if (!StringUtil.isEmpty(productLineCode)) {
					userList.addAll(mpmProjectVisitExpertService.queryUserByProductLine(productLineCode));
				}
			}
		}
		if (CollectionUtil.isEmpty(userList)) {
			userList.add("2355");
		}
		// 对重复人员进行过滤
		return userList.stream().distinct().collect(Collectors.toList());
	}
}
